package com.common.app.widget.calendarPickerView

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.common.app.R
import com.common.app.data.bean.CalendarDay
import com.common.base.app.extras.yes

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：17/7/2025
 *
 * 描述：日历单元格视图
 *
 * 修订历史：
 *
 */
class CalendarDayView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
    private var showGreyPoint: Boolean = true
) : View(context, attrs, defStyleAttr) {

    private var calendarDay: CalendarDay? = null
    private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val backgroundPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val eventPaint = Paint(Paint.ANTI_ALIAS_FLAG)

    private val normalTextColor = ContextCompat.getColor(context, R.color.text_content)
    private val weekTextColor = ContextCompat.getColor(context, R.color.text_content_week)
    private val selectedTextColor = ContextCompat.getColor(context, R.color.white)
    private val todayTextColor = ContextCompat.getColor(context, R.color.product_color)
    private val selectedBackgroundColor = ContextCompat.getColor(context, R.color.product_color)
    private var eventColor = ContextCompat.getColor(context, R.color.product_color)

    // 按照 calendar_day_item_layout 的尺寸规格
    private val dayTextSize = resources.getDimensionPixelSize(R.dimen.dp_32)
    private val eventDotSize = resources.getDimensionPixelSize(R.dimen.dp_6)
    private val eventDotRadius = resources.getDimensionPixelSize(R.dimen.dp_3).toFloat()
    private val marginVertical = resources.getDimensionPixelSize(R.dimen.dp_6)

    init {
        textPaint.textSize = resources.getDimensionPixelSize(R.dimen.sp_14).toFloat()
        textPaint.textAlign = Paint.Align.CENTER

        backgroundPaint.style = Paint.Style.FILL

        eventPaint.style = Paint.Style.FILL
        eventPaint.color = eventColor
    }

    fun setCalendarDay(day: CalendarDay) {
        this.calendarDay = day
        invalidate()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        val day = calendarDay ?: return
        val centerX = width / 2f

        // 如果不是当前月份的日期，只占位不绘制任何内容
        if (!day.isCurrentMonth) {
            return
        }

        // 按照布局文件的规格，日期文字区域在顶部32dp高度内居中
        val dayTextCenterY = dayTextSize / 2f

        // 绘制选中背景 - 32dp圆形背景
        if (day.isSelected) {
            backgroundPaint.color = selectedBackgroundColor
            canvas.drawCircle(centerX, dayTextCenterY, dayTextSize / 2f, backgroundPaint)
        }

        // 设置文字颜色
        textPaint.color = when {
            day.isSelected -> selectedTextColor
            day.isToday -> todayTextColor
            else -> normalTextColor
        }

        // 绘制日期文字 - 在32dp区域内居中
        val textY = dayTextCenterY - (textPaint.descent() + textPaint.ascent()) / 2
        canvas.drawText(day.day.toString(), centerX, textY, textPaint)

        showGreyPoint.yes {
            // 绘制事件标记点 - 在日期文字下方，按照布局文件的位置
            eventColor = if (day.hasEvent) {
                ContextCompat.getColor(context, R.color.product_color)
            }else{
                ContextCompat.getColor(context, R.color.color_EBEBEB)
            }

            eventPaint.color = eventColor
            val dotY = dayTextSize + marginVertical + eventDotRadius
            canvas.drawCircle(centerX, dotY, eventDotRadius, eventPaint)
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        // 高度 = 日期文字区域(32dp) + 上下边距(6dp) + 事件点区域(6dp) + 上下边距(6dp)
        val width = dayTextSize
        val height = dayTextSize + marginVertical * 2 + eventDotSize
        setMeasuredDimension(width, height)
    }
}